#include "../head/client.h"
#include <crypt.h>
#include <pwd.h>


int recvmasge(int netfd,TLVMsg *msg)
{
    bzero(msg,sizeof(TLVMsg));
    int rest=0;
    rest=recv(netfd,&msg->type,sizeof(int),MSG_WAITALL);
    if(rest==0)
    {
        return -1;
    }
    rest=recv(netfd,&msg->value_len,sizeof(int),MSG_WAITALL);
    if(rest==0)
    {
        return -1;
    }
    rest=recv(netfd,&msg->value,msg->value_len,MSG_WAITALL);
    if(rest==0)
    {
        return -1;
    }

    // msg->value[msg->value_len]='\0'; //确保字符串结束
    printf("收到消息：type=%d, value_len=%d, value=%s\n", msg->type, msg->value_len, msg->value);
    return 0;

}

int sendmasge(int netfd,int cont, int len,const char *buf)
{
    TLVMsg sendmsg;
    bzero(&sendmsg,sizeof(TLVMsg));
    sendmsg.type=cont;
    sendmsg.value_len=len;
    memcpy(sendmsg.value, buf, len);
    sendmsg.value[sendmsg.value_len]='\0';

    send(netfd,&sendmsg.type,sizeof(int),MSG_NOSIGNAL);
    send(netfd,&sendmsg.value_len,sizeof(int),MSG_NOSIGNAL);
    send(netfd,&sendmsg.value,sendmsg.value_len,MSG_NOSIGNAL);
    printf("type:%d,len:%d,value:%s\n",sendmsg.type,sendmsg.value_len,sendmsg.value);
    return 0;

}



int clicent_sign_in(const char *user,char *passwd,int sockfd)
{
    TLVMsg usermsg;
    bzero(&usermsg,sizeof(usermsg));
    //1.客户端发送name; 
    sendmasge(sockfd,CMD_SIGN,strlen(user),user);
    
    //2.接收服务端发送盐值
    char salt[33];
    recvmasge(sockfd,&usermsg);
    strcpy(salt,usermsg.value);
    printf("salt----asdfghjk--=%s\n",salt);
    if(salt[0]!='$'){
        printf("salt------=%s\n",salt);
        return -1;
    }
    //3.客户端发送密文
    //char buf[65];
    struct crypt_data data;
    data.initialized=0;

    char * crypt_pass=crypt_r(passwd,salt,&data);
   
    printf("%s\n",crypt_pass); 
    sendmasge(sockfd,CMD_SIGN,strlen(crypt_pass),crypt_pass);
    //revs(netfd,user->hashed_password,sizeof(user->hashed_password),MSG_WAITALL);

    //4.收-----杜------
    bzero(&usermsg,sizeof(usermsg));
    recvmasge(sockfd,&usermsg);
    printf("%s\n",usermsg.value);

  

    return 0;
}




//passwd--明文
int clicent_login_in(char *user,char *passwd ,int sockfd)
{
    TLVMsg usermsg;
    bzero(&usermsg,sizeof(usermsg));
    //1.客户端发送name; 
    sendmasge(sockfd,CMD_SIGN,strlen(user),user);
    
    //2.判断是否有用户
    recvmasge(sockfd,&usermsg);
    if(strcmp("1",usermsg.value)==0)
    {
        printf("username havenot please sign in username.value=%s\n",usermsg.value);
        return 0;
    }else if(strcmp("1",usermsg.value)==1){}
    else{
        printf("server is error\n");
    }


    //2.接收服务端发送盐值
    char salt[33];
    recvmasge(sockfd,&usermsg);
    strcpy(salt,usermsg.value);
    //3.客户端发送密文
    char buf[65];
    snprintf(buf,sizeof(buf),"%s",crypt(passwd,salt));
    printf("%s",buf);
    
    sendmasge(sockfd,CMD_SIGN,sizeof(buf),buf);
    //revs(netfd,user->hashed_password,sizeof(user->hashed_password),MSG_WAITALL);

    //4.收-----杜------
    /* bzero(&usermsg,sizeof(usermsg)); */
    /* recvmasge(sockfd,&usermsg); */
    /* printf("%s\n",usermsg.value); */
    return 0;
}

